#include "system.h"
#include "..\CGraphics\CGraphics.h"


void ParseListCommand(char *Parameter){
    char UpParam[100];
    char Temp[105];
    char STR[250];
    nodeAddress N=FIRST(NODELIST),NLoop;
    edgeAddress E=FIRST(EDGELIST);
    ownerAddress O;
    int Down;
    TabelMhs TMhs;
    TabelMK TMK;
    TabelPMK TPMK;
    int Count;
    char SITES[15]=" sites ";
    char SERVERS[15]=" servers ";
    char CLIENTS[15]=" clients ";
    char TABLES[15]=" tables ";
    char CONNECTIONS[25]=" connections ";
    char ASC[10] = " asc ";
    char DESC[10] = " desc ";
    CopyStr(Temp, Parameter);
    StartPrintProcess();
    AddChrF(Temp,' ');
    AddChr(Temp,' ');
    UpperStr(UpParam,Temp); 
    
    
    if (DetectFalseParameter(Temp)){
        printf("Parameter yang tak diproses : ");
        PreProcessParameter(Temp);
        ContinuePrintProcess();    
        ContinuePrintProcess();                             
    }
    
    if (!ExistSubNoCase(UpParam,SITES)&&!ExistSubNoCase(UpParam,SERVERS)&&
    !ExistSubNoCase(UpParam,CLIENTS)&&!ExistSubNoCase(UpParam,TABLES)&&
    !ExistSubNoCase(UpParam,CONNECTIONS)) {
       AddStr(UpParam," SITES SERVERS CLIENTS TABLES CONNECTIONS ");
       printf("Tidak ada parameter data yang sesuai/Parameter data Kosong. Printing secara default");
       ContinuePrintProcess();
       printf("Parameter yang bisa dipakai : sites connections servers clients tables asc desc");
       ContinuePrintProcess();
       ContinuePrintProcess();
    }
    
    if (!ExistSubNoCase(UpParam,ASC)&&!ExistSubNoCase(UpParam,DESC)) {
          printf("Tidak ada parameter Ascending/Descending. Printing List secara Ascending :");
          AddStr(UpParam," ASC ");
          ContinuePrintProcess();
          ContinuePrintProcess();
       }
       else if (ExistSubNoCase(UpParam,ASC)&&ExistSubNoCase(UpParam,DESC)) {
          printf("Tidak bisa printing Ascending-Descending bersamaan.");
          ContinuePrintProcess();
          printf("Printing sekarang secara Ascending");
          ContinuePrintProcess();
          ContinuePrintProcess();                            
       }
       else if (ExistSubNoCase(UpParam,ASC)) {
          printf("Terdapat parameter Ascending. Printing secara Ascending");
          ContinuePrintProcess();
          ContinuePrintProcess();                              
       }
       else if (ExistSubNoCase(UpParam,DESC)) {
          printf("Terdapat parameter Descending. Printing secara Descending");  
          ContinuePrintProcess();
          ContinuePrintProcess();                            
       }
       
    if (ExistSubNoCase(UpParam,SITES)){
        SetColor(FCI);
                                
        AddStr(UpParam," SERVERS CLIENTS TABLES");                                
        printf("Sites :");
        ContinuePrintProcess();
        sprintf(STR,"Site");
        AddSpace(STR,"site");
        printf("%s",STR);
        if (ExistSubNoCase(UpParam,SERVERS)){
           sprintf(STR,"Server");
           AddSpace(STR,"server");
           printf("%s",STR);
        }
        if (ExistSubNoCase(UpParam,CLIENTS)){
           sprintf(STR,"Client");
           AddSpace(STR,"client");
           printf("%s",STR);
        }
        sprintf(STR,"Retrieve");
        AddSpace(STR,"retrieve");
        printf("%s",STR); 
        sprintf(STR,"Update");
        AddSpace(STR,"update");
        printf("%s",STR);
        sprintf(STR,"Insert");
        AddSpace(STR,"insert");
        printf("%s",STR);
        sprintf(STR,"Delete");
        AddSpace(STR,"delete");
        printf("%s",STR);
        if (ExistSubNoCase(UpParam,TABLES)){
           sprintf(STR,"Tables");
           AddSpace(STR,"table");
           printf("%s",STR);
           sprintf(STR,"No.of Rec");
           AddSpace(STR,"rec");
           printf("%s",STR);
        }          
        ContinuePrintProcess();
        
        SetColor(FWI);
                     
        PrintSites(UpParam,N);           
    }
    else {
       if (ExistSubNoCase(UpParam,SERVERS)){
          SetColor(FCI);                                  
          printf("Servers :");
          SetColor(FWI);
          ContinuePrintProcess();
          if (ExistSubNoCase(UpParam,DESC)&&!ExistSubNoCase(UpParam,ASC))                                   
             PrintAdHoc(" SERVERS DESC ", FIRST(NODELIST));  
          else  
             PrintAdHoc(" SERVERS ASC ", FIRST(NODELIST));  
          ContinuePrintProcess();                           
       }  
       if (ExistSubNoCase(UpParam,CLIENTS)){
          SetColor(FCI);                                  
          printf("Clients :");
          SetColor(FWI);
          ContinuePrintProcess();
          if (ExistSubNoCase(UpParam,DESC)&&!ExistSubNoCase(UpParam,ASC))                                   
             PrintAdHoc(" CLIENTS DESC ", FIRST(NODELIST));  
          else  
             PrintAdHoc(" CLIENTS ASC ", FIRST(NODELIST));   
          ContinuePrintProcess();                                    
       }
       if  (ExistSubNoCase(UpParam,TABLES)){
          SetColor(FCI);
          sprintf(STR,"Tables");
          AddSpace(STR,"table");
          printf("%s",STR); 
          
          sprintf(STR,"No.of Rec");
          AddSpace(STR,"rec");
          printf("%s",STR);
          SetColor(FWI);
          ContinuePrintProcess();
          
          Count=0;
          sprintf(STR,"Mahasiswa");
          AddSpace(STR,"table");
          printf("%s",STR);
          TMhs=FIRST(TABELMHS);
          if (TMhs!=NULL){
              while (TMhs!=NULL){
                TMhs=NEXT(TMhs);
                Count++;
              }      
          }  
          printf("%d",Count);
          ContinuePrintProcess();
          
          Count=0;
          sprintf(STR,"MataKuliah");
          AddSpace(STR,"table");
          printf("%s",STR);
          TMK=FIRST(TABELMK);
          if (TMK!=NULL){
              while (TMK!=NULL){
                TMK=NEXT(TMK);
                Count++;
              }      
          }  
          printf("%d",Count);
          ContinuePrintProcess(); 
          
          Count=0;
          sprintf(STR,"PengambilanMK");
          AddSpace(STR,"table");
          printf("%s",STR);
          TPMK=FIRST(TABELPMK);
          if (TPMK!=NULL){
              while (TPMK!=NULL){
                TPMK=NEXT(TPMK);
                Count++;
              }      
          }  
          printf("%d",Count);
          ContinuePrintProcess();  
       }
         
    }
    ContinuePrintProcess();
    if (ExistSubNoCase(UpParam,CONNECTIONS)){
        SetColor(FCI);                                     
        printf("Connections :"); 
        ContinuePrintProcess();      
        sprintf(STR,"Site-1");
        AddSpace(STR,"site");
        printf("%s",STR);
        sprintf(STR,"Site-2");
        AddSpace(STR,"site");
        printf("%s",STR);
        sprintf(STR,"Download");
        AddSpace(STR,"retrieve");
        printf("%s",STR);
        sprintf(STR,"Upload");
        AddSpace(STR,"retrieve");
        printf("%s",STR);  
        ContinuePrintProcess();  
        SetColor(FWI);
        PrintEdges(UpParam,FIRST(EDGELIST));                       
    }
     
}

void PrintSites(char *Parameter,nodeAddress N){
    char UpParam[100];
    edgeAddress E=FIRST(EDGELIST);
    ownerAddress O;
    int Down;
    TabelMhs TMhs;
    TabelMK TMK;
    TabelPMK TPMK;
    int Count;
    CopyStr(UpParam,Parameter);
    char SITES[15]=" sites ";
    char SERVERS[15]=" servers ";
    char CLIENTS[15]=" clients ";
    char TABLES[15]=" tables ";
    char CONNECTIONS[25]=" connections ";
    char ASC[10] = " asc ";
    char DESC[10] = " desc ";
    char STR[250];
    
    if (N!=NULL) {
    if (ExistSubNoCase(UpParam,DESC)&&!ExistSubNoCase(Parameter,ASC)){
        PrintSites(UpParam,NEXT(N));                                  
    }             
    sprintf(STR,"%s",NODENAME(N).s);
    AddSpace(STR,"site");
    printf("%s",STR);
            if (ExistSubNoCase(UpParam,SERVERS)){
                HAS_SERVER(N)?sprintf(STR,"Ya"):sprintf(STR,"Tidak");
                AddSpace(STR,"server");
                printf("%s",STR);                               
            }  
            if (ExistSubNoCase(UpParam,CLIENTS)){
                HAS_CLIENT(N)?sprintf(STR,"Ya"):sprintf(STR,"Tidak");
                AddSpace(STR,"client");
                printf("%s",STR);  
            }
            if (HAS_SERVER(N)){
                sprintf(STR,"%.4f",RETRIEVETIME(N));
                AddSpace(STR,"retrieve");
                printf("%s",STR);
                sprintf(STR,"%.4f",UPDATETIME(N));
                AddSpace(STR,"update");
                printf("%s",STR);
                sprintf(STR,"%.4f",INSERTTIME(N));
                AddSpace(STR,"insert");
                printf("%s",STR);
                sprintf(STR,"%.4f",DELETETIME(N));
                AddSpace(STR,"delete");
                printf("%s",STR); 
            }
            if (ExistSubNoCase(UpParam,TABLES)){
                Down=0;                                 
                if (HAS_TMHS(N)||HAS_TMK(N)||HAS_TPMK(N)){
                   if (HAS_TMHS(N)) {
                      Count=CountNodeRecordMhs(TABELMHS,N);
                      sprintf(STR,"Mahasiswa");
                      AddSpace(STR,"table");
                      printf("%s",STR);
                      printf("%d",Count); 
                      Down=1;             
                   }                    
                   if (HAS_TMK(N)) {
                      Count=CountNodeRecordMK(TABELMK,N);     
                      if (Down){  
                          ContinuePrintProcess(); 
                          sprintf(STR,"");
                          AddSpace(STR,"site");
                          printf("%s",STR); 
                          sprintf(STR,"");
                          AddSpace(STR,"retrieve");
                          printf("%s",STR);
                          sprintf(STR,"");
                          AddSpace(STR,"update");
                          printf("%s",STR);
                          sprintf(STR,"");
                          AddSpace(STR,"insert");
                          printf("%s",STR);
                          sprintf(STR,"");
                          AddSpace(STR,"delete");
                          printf("%s",STR);
                                 
                          if (ExistSubNoCase(UpParam,SERVERS)){
                              sprintf(STR,"");
                              AddSpace(STR,"server");
                              printf("%s",STR);                              
                          }  
                          if (ExistSubNoCase(UpParam,CLIENTS)){
                              sprintf(STR,"");
                              AddSpace(STR,"client");
                              printf("%s",STR); 
                          }               
                      }             
                      sprintf(STR,"MataKuliah");
                      AddSpace(STR,"table");
                      printf("%s",STR); 
                      printf("%d",Count); 
                      Down=1;             
                   }      
                   if (HAS_TPMK(N)) {
                      Count=CountNodeRecordPMK(TABELPMK,N);          
                      if (Down){  
                          ContinuePrintProcess(); 
                          sprintf(STR,"");
                          AddSpace(STR,"site");
                          printf("%s",STR); 
                          sprintf(STR,"");
                          AddSpace(STR,"retrieve");
                          printf("%s",STR);
                          sprintf(STR,"");
                          AddSpace(STR,"update");
                          printf("%s",STR);
                          sprintf(STR,"");
                          AddSpace(STR,"insert");
                          printf("%s",STR);
                          sprintf(STR,"");
                          AddSpace(STR,"delete");
                          printf("%s",STR);
                                 
                          if (ExistSubNoCase(UpParam,SERVERS)){
                              sprintf(STR,"");
                              AddSpace(STR,"server");
                              printf("%s",STR);                              
                          }  
                          if (ExistSubNoCase(UpParam,CLIENTS)){
                              sprintf(STR,"");
                              AddSpace(STR,"client");
                              printf("%s",STR); 
                          }               
                      }                 
                      sprintf(STR,"PengambilanMK\t");
                      AddSpace(STR,"table");
                      printf("%s",STR); 
                      printf("%d",Count);              
                   }                
                }
            }   
            ContinuePrintProcess();
            if (ExistSubNoCase(UpParam,"asc")){
        PrintSites(UpParam,NEXT(N));                                  
    } 
    }
    
}

void PrintEdges(char *Parameter,edgeAddress E){ 
     char UpParam[100];
     CopyStr(UpParam,Parameter);
     char ASC[10] = " asc ";
     char DESC[10] = " desc ";
     char STR[250];
     if (E!=NULL){
         if (ExistSubNoCase(UpParam,DESC)&&!ExistSubNoCase(UpParam,ASC)){
             PrintEdges(UpParam,NEXT(E));                             
         }        
         sprintf(STR,"%s",NODENAME(GetA(E)).s);
         AddSpace(STR,"site");
         printf("%s",STR);
         sprintf(STR,"%s",NODENAME(GetB(E)).s);
         AddSpace(STR,"site");
         printf("%s",STR); 
         sprintf(STR,"%.4f",GetDownload(E));
         AddSpace(STR,"retrieve");
         printf("%s",STR); 
         sprintf(STR,"%.4f",GetUpload(E));
         AddSpace(STR,"retrieve");
         printf("%s",STR); 
         
         ContinuePrintProcess();
         if (ExistSubNoCase(UpParam,ASC)){
             PrintEdges(UpParam,NEXT(E));                             
         } 
     }
}

void PrintAdHoc(char *Parameter, nodeAddress N){
    char SITES[15]=" sites ";
    char SERVERS[15]=" servers ";
    char CLIENTS[15]=" clients ";
    char TABLES[15]=" tables ";
    char CONNECTIONS[25]=" connections ";
    char ASC[10] = " asc ";
    char DESC[10] = " desc ";
    if (N!=NULL) {
        if (ExistSubNoCase(Parameter,DESC)&&!ExistSubNoCase(Parameter,ASC)){
            PrintAdHoc(Parameter,NEXT(N));
        }
            
        if (ExistSubNoCase(Parameter,SERVERS)) {
            if (HAS_SERVER(N)){
                printf(NODENAME(N).s);   
                ContinuePrintProcess();  
            }                               
        }           
        else if (ExistSubNoCase(Parameter,CLIENTS)){
            if (HAS_CLIENT(N)) {
                printf(NODENAME(N).s);  
                ContinuePrintProcess();  ;
            }
        }   
        else if (ExistSubNoCase(Parameter,TABLES)){
                                               
        }
        if (ExistSubNoCase(Parameter,ASC)){
            PrintAdHoc(Parameter,NEXT(N));   
        }                       
    } 
}

void PreProcessParameter(char *Parameter){
    char DATA[10][25]; 
    char STR[100];
    CopyStr(DATA[0]," sites ");
    CopyStr(DATA[1]," servers ");
    CopyStr(DATA[2]," clients ");
    CopyStr(DATA[3]," tables ");
    CopyStr(DATA[4]," connections ");
    CopyStr(DATA[5]," asc ");
    CopyStr(DATA[6]," desc "); 
    int L,CC=0, B,Count;
    
    CopyStr(STR,"");
    while (CC<LongStr(Parameter)){
       B=0; 
       if (Parameter[CC]!=' ')
          AddChr(STR,Parameter[CC]);
       else if ((LongStr(STR)>0)&&(Parameter[CC]==' ')) {
            AddChr(STR,Parameter[CC]);
             Count=0;  
             for (L=0;L<=6;L++) {
                if (ExistSubNoCase(DATA[L],STR))
                  Count++;
             }
             if (Count==0)
                B=1;
            if (B) {
               DelChrF(STR);    
               printf("%s",STR);
            }    
            CopyStr(STR,"");
          }
       if ((LongStr(STR)==0)&&(Parameter[CC]==' ')){   
          AddChr(STR,Parameter[CC]); 
       }
       CC++;
    }
}

int DetectFalseParameter(char *Parameter){
    char DATA[10][25];
    char STR[100]; 
    CopyStr(DATA[0]," sites ");
    CopyStr(DATA[1]," servers ");
    CopyStr(DATA[2]," clients ");
    CopyStr(DATA[3]," tables ");
    CopyStr(DATA[4]," connections ");
    CopyStr(DATA[5]," asc ");
    CopyStr(DATA[6]," desc "); 
    int L,CC=0, B=0,Count;
    CopyStr(STR,"");
    while ((CC<LongStr(Parameter))&&(!B)){
          if (Parameter[CC]!=' ')
             AddChr(STR,Parameter[CC]);
          else if ((LongStr(STR)>0)&&(Parameter[CC]==' ')) {
            AddChr(STR,Parameter[CC]);
             Count=0;  
             for (L=0;L<=6;L++) {
                if (ExistSubNoCase(DATA[L],STR))
                  Count++;
             }
             if (Count==0)
                B=1;
            CopyStr(STR,"");
          }
       if ((LongStr(STR)==0)&&(Parameter[CC]==' ')){   
          AddChr(STR,Parameter[CC]); 
       }
       CC++;
    }
    return B;
}
